﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.UserAd
{
    public partial class MyAdPosition : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadContent();
            }
        }

        #region 加载内容
        private void LoadContent()
        {
            Users uinfo=UserBase.GetCurrentUser();

            AdPositionHelper aphelper = new AdPositionHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "Type", 1);

            string[] fields = new string[] { "ID", "AdPositionName", "LinkURL", "Remark", "Price", "Orders", "Type", "Flag" };

            DataTable dt = new DataTable();
            dt = aphelper.QueryAdPositionBySearchToTable(c, new Order[] { new Order("Orders", OrderMode.Asc) }, 0, 0, fields);

            dt.Columns.Add(new DataColumn("strUserAdBeginDate"));
            dt.Columns.Add(new DataColumn("strUserAdEndDate"));
            dt.Columns.Add(new DataColumn("Status"));
            dt.Columns.Add(new DataColumn("UserAdPositionID"));
            dt.Columns.Add(new DataColumn("strPrice"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    int price = Convert.ToInt32(dr["Price"] + "");
                    if (price > 0)
                    {
                        dr["strPrice"] = price + "个/月";
                    }
                    else
                    {
                        dr["strPrice"] = "免费";
                    }
                    string adpositionid = dr["ID"] + "";
                    //判断该广告位用户是否购买
                    UserAdPositionHelper uadhelper = new UserAdPositionHelper();
                    Criteria cu = new Criteria();
                    cu.Add(CriteriaType.Equals, "AdPositionID", adpositionid);
                    cu.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
                    List<UserAdPosition> uplist = uadhelper.QueryUserAdPositionBySearch(cu, null);
                    if (uplist != null && uplist.Count > 0)
                    {
                        //该广告位该用户已经购买
                        DateTime dtBeginDate = Convert.ToDateTime(uplist[0].BeginDate);//续费日期
                        DateTime dtEndDate = Convert.ToDateTime(uplist[0].EndDate);//到期日期

                        dr["strUserAdBeginDate"] = dtBeginDate.ToString("yyyy-MM-dd HH:mm");
                        dr["strUserAdEndDate"] = dtEndDate.ToString("yyyy-MM-dd HH:mm");

                        //出现续费按钮
                        dr["Status"] = 1;
                        dr["UserAdPositionID"] = uplist[0].ID;
                    }
                    else
                    {
                        //该广告位该用户没有购买
                        dr["strUserAdBeginDate"] = "";
                        dr["strUserAdEndDate"] = "";
                        //出现购买按钮
                        dr["Status"] = 0;
                        dr["UserAdPositionID"] = "";
                    }
                }
            }

            rptAdPosition.DataSource = dt;
            rptAdPosition.DataBind();

            for (int i = 0; i < rptAdPosition.Items.Count; i++)
            {
                HiddenField hidStatus = (HiddenField)rptAdPosition.Items[i].FindControl("hidStatus");
                LinkButton lbReBuy = (LinkButton)rptAdPosition.Items[i].FindControl("lbReBuy");
                LinkButton lbBuy = (LinkButton)rptAdPosition.Items[i].FindControl("lbBuy");

                string status = hidStatus.Value;
                if (status == "0")//购买
                {
                    lbBuy.Visible = true;
                    lbReBuy.Visible = false;
                }
                else if(status=="1")//续费
                {
                    lbReBuy.Visible = true;
                    lbBuy.Visible = false;
                }
            }
        }
        #endregion

        #region 列表事件
        protected void rptAdPosition_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            HiddenField hidPrice = (HiddenField)e.Item.FindControl("hidPrice");
            HiddenField hidAdPositionName = (HiddenField)e.Item.FindControl("hidAdPositionName");
            HiddenField hidOrders = (HiddenField)e.Item.FindControl("hidOrders");
            HiddenField hidUserAdPositionID = (HiddenField)e.Item.FindControl("hidUserAdPositionID");

            string positionid = hidID.Value;
            int price = Convert.ToInt32(hidPrice.Value);//广告位价钱
            string adpositionname = hidAdPositionName.Value;
            string useradpositionid=hidUserAdPositionID.Value;
            int orders = Convert.ToInt32(hidOrders.Value);

            if (e.CommandName == "buy")//购买
            {
                if (price == 0)//免费
                {
                    Users uinfo = UserBase.GetCurrentUser();
                    //新增用户广告位记录
                    UserAdPosition uapinfo = new UserAdPosition();
                    UserAdPositionHelper adphelper = new UserAdPositionHelper();
                    uapinfo.UsersID = uinfo.ID;
                    uapinfo.NickName = uinfo.NickName;
                    uapinfo.AdPositionID = positionid;
                    uapinfo.Orders = orders;
                    uapinfo.AdPositionName = adpositionname;
                    uapinfo.BeginDate = System.DateTime.Now;
                    uapinfo.EndDate = System.DateTime.Now.AddDays(30);
                    adphelper.AddUserAdPosition(uapinfo);
                    Tools.Alert("购买成功");
                }
                else
                {
                    //判断用户当前余额
                    UsersHelper uhelper = new UsersHelper();
                    Users uinfo = UserBase.GetCurrentUser();
                    float currentmoney = uhelper.GetCurrentMoney(uinfo.ID);
                    if (currentmoney >= price)//购买
                    {
                        //去除现金
                        CashFlow cfinfo = new CashFlow();
                        CashFlowHelper cfhelper = new CashFlowHelper();
                        cfinfo.UsersID = uinfo.ID;
                        cfinfo.Money = price * -1;
                        cfinfo.Type = 6;
                        cfinfo.Description = "广告位购买：" + adpositionname;
                        cfhelper.AddCashFlow(cfinfo);
                        //新增用户广告位记录
                        UserAdPosition uapinfo = new UserAdPosition();
                        UserAdPositionHelper adphelper = new UserAdPositionHelper();
                        uapinfo.UsersID = uinfo.ID;
                        uapinfo.NickName = uinfo.NickName;
                        uapinfo.AdPositionID = positionid;
                        uapinfo.Orders = orders;
                        uapinfo.AdPositionName = adpositionname;
                        uapinfo.BeginDate = System.DateTime.Now;
                        uapinfo.EndDate = System.DateTime.Now.AddDays(30);
                        adphelper.AddUserAdPosition(uapinfo);
                        Tools.Alert("购买成功");
                    }
                    else//当前金额不够，提示进行充值
                    {
                        Tools.Alert("您当前友赢豆数不足，请先进行充值");
                        return;
                    }
                }
            }
            else if (e.CommandName == "rebuy")//续费
            {
                if (price == 0)//免费
                {
                    //用户广告位记录更新
                    UserAdPosition uapinfo = new UserAdPosition();
                    UserAdPositionHelper uaphelper = new UserAdPositionHelper();
                    uapinfo = uaphelper.GetUserAdPosition(useradpositionid);
                    DateTime dtenddate = uapinfo.EndDate;

                    uapinfo.BeginDate = System.DateTime.Now;
                    if (dtenddate > System.DateTime.Now)
                    {
                        uapinfo.EndDate = dtenddate.AddDays(30);
                    }
                    else
                    {
                        uapinfo.EndDate = System.DateTime.Now.AddDays(30);
                    }
                    uaphelper.EditUserAdPosition(uapinfo);

                    Tools.Alert("续费成功");
                }
                else
                {
                    //判断用户当前余额
                    UsersHelper uhelper = new UsersHelper();
                    Users uinfo = UserBase.GetCurrentUser();
                    float currentmoney = uhelper.GetCurrentMoney(uinfo.ID);
                    if (currentmoney >= price)//续费
                    {
                        CashFlow cfinfo = new CashFlow();
                        CashFlowHelper cfhelper = new CashFlowHelper();
                        cfinfo.UsersID = uinfo.ID;
                        cfinfo.Money = price * -1;
                        cfinfo.Type = 6;
                        cfinfo.Description = "广告位续费：" + adpositionname;
                        cfhelper.AddCashFlow(cfinfo);

                        //用户广告位记录更新
                        UserAdPosition uapinfo = new UserAdPosition();
                        UserAdPositionHelper uaphelper = new UserAdPositionHelper();
                        uapinfo = uaphelper.GetUserAdPosition(useradpositionid);
                        DateTime dtenddate = uapinfo.EndDate;

                        uapinfo.BeginDate = System.DateTime.Now;
                        if (dtenddate > System.DateTime.Now)
                        {
                            uapinfo.EndDate = dtenddate.AddDays(30);
                        }
                        else
                        {
                            uapinfo.EndDate = System.DateTime.Now.AddDays(30);
                        }
                        uaphelper.EditUserAdPosition(uapinfo);

                        Tools.Alert("续费成功");
                    }
                    else//当前金额不够，提示进行充值
                    {
                        Tools.Alert("您当前友赢豆数不足，请先进行充值");
                        return;
                    }
                }
            }
            LoadContent();                   
        }
        #endregion
    }
}